package com.ruoyi.common.utils;
import java.security.MessageDigest;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
/**
 * 签名算法 HMAC/SHA1、HMAC/SHA256
 * @author DEREK
 *
 */
public class HmacSha {

	// 将加密后的字节数组转换成字符串
	public static String bytesToHex(byte[] bytes) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < bytes.length; i++) {
			String hex = Integer.toHexString(bytes[i] & 0xFF);
			if (hex.length() < 2) {
				sb.append(0);
			} 
			sb.append(hex);
		} 
		return sb.toString();
	}

	//HMAC/SHA256 加密，返回加密后的字符串
	public static String hmacSha256(String message, String secret) {
		try {
			SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes("utf-8"), "HmacSHA256");
			Mac mac = Mac.getInstance("HmacSHA256");
			mac.init(signingKey); 
			byte[] rawHmac = mac.doFinal(message.getBytes("utf-8"));
			return bytesToHex(rawHmac);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	} 
	
	//HMAC/SHA1 加密，返回加密后的字符串
    public static String hmacSha1(String message, String secret) {
        try {
            SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes("utf-8"), "HmacSHA1");
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(signingKey); 
            byte[] rawHmac = mac.doFinal(message.getBytes("utf-8"));
            return bytesToHex(rawHmac);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } 
    
    /**
     * @Comment SHA1实现
     * @Author Ron
     * @Date 2017年9月13日 下午3:30:36
     * @return
     */
    public static String shaEncode(String inStr) throws Exception {
        MessageDigest sha = null;
        try {
            sha = MessageDigest.getInstance("SHA");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }

        byte[] byteArray = inStr.getBytes("UTF-8");
        byte[] md5Bytes = sha.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }
	public static void main(String[] args) throws Exception {
        //拿到消息通知的 raw request body 并对其计算签名，也就是说下面代码中的 request_body 是反序列化之前的 binary byte array，不是反序列化之后的 object
//        String request_body = "{\"noticeId\":\"3ea765c170de41d0a3ba1d805fc5fe62\",\"productId\":1,\"eventType\":102,\"notifyMs\":1692002908104,\"payload\":{\"channelName\":\"test_webhook\",\"ts\":1560853385}}";
//        String secret = "6S5KeXHUZ";
//        System.out.println(hmacSha1(request_body, secret)); //de96da5acf03b0021ac3b4fa2225e7ae6f3533a30d50bb02c08ea4fa748bda24
		
		String nonce = OrderNumberGeneratorUtil.get();
		String timestamp = System.currentTimeMillis()+"";
		System.out.println(nonce);
		System.out.println(timestamp);
		String shaEncode = shaEncode(ProjectConstant.RONG_CLOUD_APP_SECRET+nonce+timestamp);
		System.out.println(shaEncode);
    }
	// e836820fb1ace738bbfd381debab0fff9fca757207e06dfff8c0edf49754c135
	// e836820fb1ace738bbfd381debab0fff9fca757207e06dfff8c0edf49754c135
	// 75b39062355a188874609eba6c58716b20285922
	// 75b39062355a188874609eba6c58716b20285922
	/**
	 * 
	 * 请求URL:/api/v1/agora/notifyAgora
头部：
Agora-Signature:75b39062355a188874609eba6c58716b20285922
Agora-Signature-V2:e836820fb1ace738bbfd381debab0fff9fca757207e06dfff8c0edf49754c135
声网回调：
{"noticeId":"3ea765c170de41d0a3ba1d805fc5fe62","productId":1,"eventType":102,"notifyMs":1692002908104,"payload":{"channelName":"test_webhook","ts":1560853385}}

请求URL:/api/v1/agora/notifyAgora
头部：
Agora-Signature:7ba6a27d7cd2636fc0fd4bdde5bc4ea5d1020efe
Agora-Signature-V2:4afa07bf7e2b0a4e934eacff10dd0c2dea9eacac33081594ebc71f67fa157912
声网回调：
{"noticeId":"97380b54913843b58508868d702358cd","productId":1,"eventType":105,"notifyMs":1692002909139,"payload":{"channelName":"test_webhook","platform":1,"ts":1560396834,"uid":12121212}}

请求URL:/api/v1/agora/notifyAgora
头部：
Agora-Signature:98171ace00bbef64127716c410844da3a5dc3928
Agora-Signature-V2:d880eaaab80ea6208596a2965cb4f9bd26b08e26753356f3c1f4e5ad6f83bfb9
声网回调：
{"noticeId":"a2e0500554e444b39bd0faea7cc61054","productId":1,"eventType":107,"notifyMs":1692002910480,"payload":{"channelName":"test_webhook","platform":1,"ts":1560396834,"uid":12121212}}

	 */
}
